package com.qf.microuser.user.controller;

import com.qf.common.core.annotation.NoLogin;
import com.qf.common.core.common.R;
import com.qf.microuser.user.param.LoginParam;
import com.qf.microuser.user.param.RegisterParam;
import com.qf.microuser.user.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 天--道--酬--勤
 *
 * @author LiangGaoQiang
 * @ClassName UserController
 * @Date: 2025/5/21 16:58
 * @Description: 描述
 */


@RestController
@Validated
@RequestMapping("/user")
public class LoginController {
    @Resource
    private UserService userService;

    /**
     * 注册
     *
     * @param registerParam 前端传递的注册参数
     * @return R对象
     */
    @PostMapping("/register")
    @NoLogin
    public R register(@Valid @RequestBody RegisterParam registerParam) {
        userService.register(registerParam);
        return R.ok();
    }


    /**
     * 登录
     *
     * @param loginParam 前端传递的登录参数
     * @return R对象
     */
    @PostMapping("/login")
    @NoLogin
    public R login(@RequestBody LoginParam loginParam) {
        String jwtToken = userService.login(loginParam);

        // 如果user为空，则登录失败
        if (jwtToken == null) {
            return R.fail();
        }
        return R.ok(jwtToken);
    }


    @GetMapping("/sendSms")
    public R getPhoneVerifyCode(String phone) {
        userService.getPhoneVerifyCode(phone);
        return R.ok();
    }

}
